import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import CrawlimgItem

class PictureSpider(CrawlSpider):
    name = 'picture'
    # allowed_domains = ['baidu.com']
    start_urls = ['https://www.qqtn.com/tp/dmtp_1.html']

    rules = (
        Rule(LinkExtractor(allow=r'/tp/dmtp_\d+.html'), callback='parse_item', follow=True),
        Rule(LinkExtractor(allow=r'/article/article_.*?.html'), callback='detial_item', follow=False),
    )

    def parse_item(self, response):
        imgs = response.xpath('//ul[@class="g-gxlist-imgbox"]//li')
        print(len(imgs))

    def detial_item(self, response):
        img_title = response.xpath('//div[@class="g-cont-detail g-main-bg"]/h1/text()').get()
        imgs1 = response.xpath('//p[@align="center"]')
        for img in imgs1:
            item = CrawlimgItem()
            item['img_title'] = img_title
            img_url = img.xpath('./img/@src').get()
            print(img_url)
            item['img_url'] = img_url
            name = img_url.split('/')[-1]
            item['name'] = name
            yield item
